package cn.wolfcode.controller;

import cn.wolfcode.domain.Rank;
import cn.wolfcode.redis.IRankRedisService;
import cn.wolfcode.service.IRankService;
import cn.wolfcode.util.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* 排行控制层
*/
@RestController
@RequestMapping("rank")
public class RankController {

    @Autowired
    private IRankService rankService;
    @Autowired
    private IRankRedisService rankRedisService;

    @GetMapping("/list")
    public Object list(){
        return JsonResult.success(rankService.list());
    }

    @GetMapping("/addRankRedis")
    public Object addRank(Long id) {
        List<Rank> ranks = rankRedisService.addRedis(id);
        return JsonResult.success(ranks);
    }
    @GetMapping("/addRank")
    public Object addRankRedis(Long id) {
        Rank rank = rankService.getById(id);
        rank.setTotal(rank.getTotal()+1);
        rankService.saveOrUpdate(rank);
        List<Rank> total = rankService.list(new QueryWrapper<Rank>().orderByDesc("total"));
        return JsonResult.success(total);
    }


}
